﻿// -----------------------------------------------------------------------
// <copyright file="ExceptionsInCommandHandlersTests.cs">
// Copyright (c) 2013 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com  andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

namespace Moveax.DataAccess.Cqrs.Highway.EF.UnitTests
{
    using System;
    using System.Threading.Tasks;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moveax.DataAccess.Cqrs.Highway.UnitTests.Model.Commands;

    [TestClass]
    public class ExceptionsInCommandHandlersTests : TestClassBase
    {
        [TestMethod]
        public void CatchingSyncException()
        {
            // Arrange
            var cmd = new AlwaysFailingSyncCommand();
            bool isExceptionCatched = false;

            // Act
            try {
                this._unitOfWorkFactory.ExecuteSingleCommand(cmd);
            }
            catch (Exception) {
                isExceptionCatched = true;
            }

            // Assert
            Assert.IsTrue(isExceptionCatched);
        }

        [TestMethod]
        public async Task CatchingAsyncException()
        {
            // Arrange
            var cmd = new AlwaysFailingAsyncCommand();
            bool isExceptionCatched = false;

            // Act
            try {
                using (var unitOfWork = this._unitOfWorkFactory.Create()) {
                    await unitOfWork.ExecuteCommandAsync(cmd);
                }
            }
            catch (Exception) {
                isExceptionCatched = true;
            }

            // Assert
            Assert.IsTrue(isExceptionCatched);
        }
    }
}
